package com.bookstore.back.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bookstore.back.entity.BookTag;
import com.bookstore.back.entity.BookTagRelation;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 图书标签关联Mapper接口
 * 
 * @author 程序猿_Ti
 * @since 2025-07-22
 */
@Mapper
public interface BookTagRelationMapper extends BaseMapper<BookTagRelation> {

    /**
     * 根据图书ID查询关联的标签
     */
    @Select("SELECT bt.* FROM book_tag bt " +
            "INNER JOIN book_tag_relation btr ON bt.id = btr.tag_id " +
            "WHERE btr.book_id = #{bookId} " +
            "ORDER BY bt.id ASC")
    List<BookTag> getTagsByBookId(@Param("bookId") Integer bookId);

    /**
     * 根据图书ID列表批量查询标签关联
     */
    @Select("<script>" +
            "SELECT btr.book_id, bt.* FROM book_tag bt " +
            "INNER JOIN book_tag_relation btr ON bt.id = btr.tag_id " +
            "WHERE btr.book_id IN " +
            "<foreach collection='bookIds' item='bookId' open='(' separator=',' close=')'>" +
            "#{bookId}" +
            "</foreach> " +
            "ORDER BY btr.book_id ASC, bt.id ASC" +
            "</script>")
    List<BookTag> getTagsByBookIds(@Param("bookIds") List<Integer> bookIds);

}
